/* See LICENSE for copyright and terms of use */

import org.evtmng.*;
import org.evtmng.events.*;
import org.evtmng.timeline.*;
import org.evtmng.tween.*;
import org.evtmng.test.FadeBoxUtil;

/**
 * This is an example of using the timeline elements, if we don't want to tween something.
 * (Although we really do.)
 *
 * Easing function taken from Robert Penner:
 *  http://www.robertpenner.com/easing/
 *
 * @author Tay Ray Chuan
 */

class org.evtmng.test.DiagonalFadeBox_tl extends FadeBoxUtil implements IRunListener	{
	private var m_bcaster:TweenListenerBroadcaster;
	private var m_tl:Timeline;
	private var m_tlm:TimelineManager;
	
	/**
	 * Entrypoint.
	 */
	public static function main() : Void {
		var inst:DiagonalFadeBox_tl = new DiagonalFadeBox_tl();
		inst.init(_root.createEmptyMovieClip("fadeboxholder", _root.getNextHighestDepth()));
		inst.run();
	}
	
	/**
	 * Creates timeline elements and hooks up itself as a listener to TimelineManager.
	 */
	public function DiagonalFadeBox_tl() {
		m_bcaster = TweenListenerBroadcaster.instance();
		m_tl = new Timeline();
		m_tlm = new TimelineManager(m_tl);
		m_tlm.setTimeInterval(INTERVAL);
	}
	
	/**
	 * Begin sequence.
	 */
	public function run():Void {
		m_tlm.runFromTime(0);
	}
	
	/**
	 * Create a tween for a clip, beginning at the time 'sum'
	 */
	private function handleTweenAddition(sum:Number, mc:MovieClip):Void {
		var tween:Tween = new Tween(sum, sum+Math.round(DURATION/INTERVAL));
		m_bcaster.addListenerForTween(this, tween);
		m_tl.addTween(tween);
	}
	
	/**
	 * Find all clips that begin at time e.tween.start(), then set their alpha value with one
	 * calculated by EASE.
	 * @param	e
	 */
	public function whileRunningTween(e:TweenEventInfo):Void {
		var start:Number = e.tween().start();
		var arr:Array = m_mcs[start];
		var i:Number = arr.length;
		var mc:MovieClip;
		var val:Number = EASE(e.time(), 99, -98, e.tween().duration());
		while(i--) {
			mc = MovieClip(arr[i]);
			mc._alpha = val;
		}
	}
}
